import csv
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey


#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    inputCSV = 'D:/My Documents/My Dropbox/GEOG 731/final project/inoutflow.csv'
    data = build_data_list(inputCSV)
    x = data[:,1]
    y = data[:,2]
    gradient, intercept, r_value, p_value, std_err = stats.linregress(x,y)
    print "Gradient and intercept", gradient, intercept
    print "R-squared", r_value**2
    print "p-value", p_value

    plt.subplot(331)
    plt.scatter()
    #plt.scatter(x, y, label = 'Original data', color ='b')
    #http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.subplot
    
    plt.plot()
    #plt.plot(x, x*b1 + b0, 'r', label='Fitted line', linewidth=1.5)
    #http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot
    
    plt.title('')
    plt.xlabel('')
    plt.ylabel('')
    #plt.axis([min(expdata20[:,0]/1000) * 0.9, max(expdata20[:,0]/1000) *1.1, min(expdata20[:,1]) * 0.9, max(expdata20[:,1]) * 1.1])
    plt.legend()

    plt.subplot(332)

    plt.show()


    